<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="房间号" prop="roomId">
        <el-input v-model="queryParams.roomId" placeholder="请输入" clearable size="small"/>
      </el-form-item>
      <el-form-item label="主持人账号" prop="creator">
        <el-input v-model="queryParams.creator" placeholder="请输入" clearable size="small"/>
      </el-form-item>
      <el-form-item label="管理人账号" prop="manager">
        <el-input v-model="queryParams.manager" placeholder="请输入" clearable size="small"/>
      </el-form-item>
      <el-form-item label="是否热门" prop="hot">
        <el-select v-model="queryParams.hot" placeholder="请选择">
          <el-option label="全部" value="" />
          <el-option label="不是" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="socialVoiceRoomList">
      <el-table-column label="房间号" align="center" prop="roomId" />
      <el-table-column label="管理人员" align="center" prop="manager" />
      <el-table-column label="房间类型" align="center" prop="roomType" />
      <el-table-column label="房间简介" align="center" prop="introduction" />
      <el-table-column label="订阅人数" align="center" prop="subscribers" />
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime?parseTime(scope.row.createTime):'--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次关厅时间" align="center" prop="closeTime">
        <template slot-scope="scope">
          <span>{{ scope.row.closeTime?parseTime(scope.row.closeTime):'--' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="主持人" align="center" prop="closeTime" />
      <!-- <el-table-column label="在麦人数" align="center" prop="closeTime" />
      <el-table-column label="当前用户数" align="center" prop="closeTime" /> -->
      <el-table-column label="当天流水" align="center" prop="closeTime" />
      <el-table-column label="累计总流水" align="center" prop="closeTime" />
      <el-table-column label="累计总流水" align="center" prop="closeTime" />
      <el-table-column label="是否推荐热门" align="center" prop="hot" >
         <template slot-scope="scope">
          <span v-if="scope.row.hot === 0">不是</span>
          <span v-else-if="scope.row.hot === 1">是</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleShowTrans(scope.row)">查看流水</el-button>
          <el-button size="mini" type="text" @click="handleClose(scope.row)">关闭</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改语音聊天室房间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="聊天室房间号" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入聊天室房间号" />
        </el-form-item>
        <el-form-item label="房间名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入房间名称" />
        </el-form-item>
        <el-form-item label="房主账号" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入房主账号" />
        </el-form-item>
        <el-form-item label="管理员账号" prop="manager">
          <el-input v-model="form.manager" placeholder="请输入管理员账号" />
        </el-form-item>
        <el-form-item label="房间缩略图" prop="thumbnail">
          <el-input v-model="form.thumbnail" placeholder="请输入房间缩略图" />
        </el-form-item>
        <el-form-item label="房间是否有效，0-不是；1-是" prop="valid">
          <el-input v-model="form.valid" placeholder="请输入房间是否有效，0-不是；1-是" />
        </el-form-item>
        <el-form-item label="是否对外可见，0-不是；1-是" prop="visible">
          <el-input v-model="form.visible" placeholder="请输入是否对外可见，0-不是；1-是" />
        </el-form-item>
        <el-form-item label="是否热门 0-不是；1-是" prop="hot">
          <el-input v-model="form.hot" placeholder="请输入是否热门 0-不是；1-是" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <Trans v-if="transVisible" ref="trans" />
  </div>
</template>

<script>
import { listSocialVoiceRoom, getSocialVoiceRoom, delSocialVoiceRoom, addSocialVoiceRoom, updateSocialVoiceRoom, exportSocialVoiceRoom } from "@/api/system/socialVoiceRoom";

import Trans from "./trans";

export default {
  name: "SocialVoiceRoom",
  components:{
    Trans
  },
  data() {
    return {
      transVisible: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 语音聊天室房间表格数据
      socialVoiceRoomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomId: undefined,
        name: undefined,
        creator: undefined,
        manager: undefined,
        thumbnail: undefined,
        valid: undefined,
        visible: undefined,
        hot: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        roomId: [
          { required: true, message: "聊天室房间号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "房间名称不能为空", trigger: "blur" }
        ],
        creator: [
          { required: true, message: "房主账号不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleShowTrans(row){
      this.transVisible = true
      this.$nextTick(() => {
        this.$refs.trans.init(row)
      })
    },
    /** 查询语音聊天室房间列表 */
    getList() {
      this.loading = true;
      listSocialVoiceRoom(this.queryParams).then(response => {
        this.socialVoiceRoomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        roomId: undefined,
        name: undefined,
        creator: undefined,
        manager: undefined,
        thumbnail: undefined,
        valid: undefined,
        visible: undefined,
        hot: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加语音聊天室房间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSocialVoiceRoom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改语音聊天室房间";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSocialVoiceRoom(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSocialVoiceRoom(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除语音聊天室房间编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSocialVoiceRoom(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有语音聊天室房间数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSocialVoiceRoom(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>